const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 获取格式化的时间（天不补0）
 */
const formatTimeArr = date => {
  const day = Math.floor(date / (3600 * 24))
  const hour = Math.floor((date - day * 24 * 3600) / 3600)
  const minute = Math.floor((date - day * 24 * 3600 - hour * 3600) / 60)
  const second = Math.floor(date - day * 24 * 3600 - hour * 3600 - minute * 60)

  return [day, hour, minute, second].map((item, index, arr) => {
    return index != 0 ? formatNumber(item) : item
  })
}

module.exports = {
  formatTime: formatTime,
  formatTimeArr:formatTimeArr
}
